package edu.ntu.cloudroid.model.data;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import edu.ntu.cloudroid.model.IDs;
import edu.ntu.cloudroid.model.IDs.UploadID;

/**
 * The public view of the location class. 
 * 
 * This contains the info to be sent back to the client. We seem unable to use the direct LocationStamp for it,
 * because the annotiations do not serialize
 * 
 * @author Milind Ganjoo, ttadinh
 * 
 */
public class LocationInfo implements DetachedData, Serializable
{
   private static final String NODE_NAME_TYPE = "type";
   private static final String NODE_NAME_FORMATTED_ADDRESS = "formatted_address";
   private static final String NODE_TYPE_STREET_ADDRESS = "street_address"; //level 0
   private static final String NODE_TYPE_PREMISE = "premise"; //level 1
   private static final String NODE_TYPE_LOCALITY = "locality"; //level 2
   private static final String NODE_TYPE_COUNTRY = "country"; //level 3

   private String name;
   private String locationName;
   private double approxLatitude;
   private double approxLongitude;
   private Date timeSeen;
   
   private boolean isAccessGranted; 
   
   private static final String serviceURL = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=%s&sensor=true";

   private static Logger logger = Logger
         .getLogger(LocationInfo.class.getName());

   public LocationInfo(boolean iag)
   {
	   this.isAccessGranted = iag;
   }

   public boolean isAccessGranted() {
	return isAccessGranted;
   }

   public LocationInfo(String name, double lt, double latt, String locName, Date ts)
   {     
	   this.isAccessGranted = true;
      this.locationName = locName;
      this.name = name;
      this.approxLatitude = latt;
      this.approxLongitude = lt;
      this.timeSeen = ts;
   }
  
   public String getLocationName(){
	   return this.locationName;
   }
    

   public String getName()
   {
      return name;
   }
  
  

   public double getApproxLatitude()
   {
      return this.approxLatitude;
   }

   public void setApproxLatitude(double approxLatitude)
   {
      this.approxLatitude = approxLatitude;
   }

   public double getApproxLongitude()
   {
      return this.approxLongitude;
   }

   public void setApproxLongitude(double approxLongitude)
   {
      this.approxLongitude = approxLongitude;
   }

   public Date getTimeSeen()
   {
      return this.timeSeen;
   }

   public void setTimeSeen(Date timeSeen)
   {
      this.timeSeen = timeSeen;
   }

	@Override
	public AbstractData toGAEData() throws Exception{
		return new LocationData(this.name, this.approxLongitude,
				this.approxLatitude, this.locationName, this.timeSeen);
	}

	@Override
	public UploadID getUploadID() {
		return IDs.UploadID.Data;
	}
	
	@Override
	public String toString(){
		return this.name+" "+this.approxLongitude+" " +this.approxLatitude+" "+this.timeSeen;
	}	
	
}
